package cn.donting.jme.utile.savable;

import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetKey;
import com.jme3.asset.AssetManager;
import com.jme3.export.*;
import org.checkerframework.checker.units.qual.A;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.IdentityHashMap;

public class TxtlJmeImporter implements JmeImporter {

    private HashMap<Integer, TxtInputCapsule> idHashMap = new HashMap<>();
    private IdentityHashMap<Savable, TxtInputCapsule> identityHashMap = new IdentityHashMap<>();
    private AssetManager assetManager;

    public TxtlJmeImporter(AssetManager assetManager) {
        this.assetManager = assetManager;
    }

    @Override
    public InputCapsule getCapsule(Savable id) {
        return null;
    }

    @Override
    public AssetManager getAssetManager() {
        return assetManager;
    }

    @Override
    public int getFormatVersion() {
        return 0;
    }

    @Override
    public Object load(AssetInfo assetInfo) throws IOException {
        return null;
    }

    public Savable load(String input) throws Exception {
        System.out.println(input);
        String[] lines = input.split("\n");
        StringBuilder context=new StringBuilder();
        String head=null;
        for (String line : lines) {
            if (line.length()==0) {
                continue;
            }
            if (line.startsWith("[#")) {
                if(head!=null ){
                    TxtInputCapsule txtInputCapsule = new TxtInputCapsule(head, context.toString());
                    int id = txtInputCapsule.getId();
                    idHashMap.put(id,txtInputCapsule);
                    Savable savable = txtInputCapsule.getSavable();
                    identityHashMap.put(savable,txtInputCapsule);
                }
                head=line;
                context.delete(0,context.length());
            }else {
                context.append(line).append("\n");
            }
        }
        return null;
    }
}
